home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #2 / Amiga Plus CD - 1995 - No. 2.iso / internet / faq / englisch / color_space < prev    next >
Encoding:
Text File  |  1995-04-11  |  61.9 KB  |  1,239 lines

  1. Archive-name: graphics/colorspace-faq
  2. Posting-Frequency: every 14 days
  3. Last-modified: 28/2/95
  4.  
  5. ###########################################################
  6. Color spaces FAQ - David Bourgin
  7. Date: 28/1/95 (Major modifications)
  8. Last update: 6/1/95 (23/1/95: minor modifications)
  9.  
  10. ---------------------------
  11.      Table of contents
  12. ---------------------------
  13. 1 - Purpose of this FAQ
  14. 2 - Some definitions
  15. 3 - What is an image based on a color look-up table (LUT)?
  16. 4 - What is this gamma component?
  17. 5 - Color space conversions
  18. 5.1 - RGB, CMY, and CMYK
  19. 5.2 - HSI and related color spaces
  20. 5.3 - CIE XYZ and gray level (monochrome included) pictures
  21. 5.4 - CIE Luv and CIE Lab
  22. 5.5 - LCH and CIE LSH
  23. 5.6 - The associated standards: YUV, YIQ, and YCbCr
  24. 5.7 - SMPTE-C RGB
  25. 5.8 - SMPTE-240M YPbPr (HD televisions)
  26. 5.9 - Xerox Corporation YES
  27. 5.10- Kodak Photo CD YCC
  28. 6 - References
  29. 7 - Comments and thanks
  30.  
  31. ---------------------------
  32.           Contents
  33. ---------------------------
  34. 1 - Purpose of this FAQ (D. Bourgin)
  35.  
  36.     I did a (too) long period of research in the video domain (video cards,
  37.     image file formats, and so on) and I've decided to provide to all people
  38.     who need some informations about that.
  39.     I aim to cover a part of the Frequently Asked Questions (FAQ) in the video
  40.     works, it means to provide some (useful?) informations about the colors,
  41.     and more especially about color spaces. If you have some informations
  42.     to ask/add to this document, please read item 7.
  43.  
  44. 2 - Some definitions (A. Ford, D.Bourgin)
  45.  
  46.     Color is defined as an experience in human perception. In physics terms, a
  47.     color is the result of an observed light on the retina of the eye. The
  48.     light must have a wavelength in the range of 400 to 700 nm. The radient
  49.     flux of observed light at each wavelength in the visible spectrum is
  50.     associated to a Spectral Power Distribution (SPD).
  51.     An SPD is created by cascading the SPD of the light source with the
  52.     Spectral Reflectance of the object in the scene. In addition the optics of
  53.     any imaging device will have an effect.
  54.     Strictly though, color is a visual sensation, so a `color' is created
  55.     when we observe a specific SPD.
  56.     We see color by means of cones in the retina. There are three types of
  57.     cones sensitive to wavelengths that approximately correspond to red, green
  58.     and blue light. Together with information from rod cells (which are not
  59.     sensitive to color) the cone information is encoded and sent to higher
  60.     brain centres along the optic nerve. The encoding, known as opponent
  61.     process theory, consists of three opponent channels, these are:
  62.  
  63.     Red     -       Green
  64.     Blue    -       Yellow
  65.     Black   -       White
  66.  
  67.     Note: Actually, recent studies show that eyes use addtionnal cone types.
  68.     (cf. "La Recherche", n.272, january, 1995)
  69.  
  70.     This is different to tri-chromatic theory (e.g. Red, Green, Blue additive
  71.     color) which you may be used to, but when we describe colors we do not say
  72.     "it is a reddy green" or "that's a bluey yellow".
  73.     Perceptually we require three attributes to describe a color. Generally
  74.     any three will do, but there need's to be three.
  75.  
  76.     For human purpose color descriptions these attributes have been by the CIE
  77.     recommendations, as follows:
  78.  
  79.     Brightness. The attribute of a visual sensation according to which an area
  80.     appears to exhibit more or less light. You can blurry or enhance an image
  81.     by modifying this attribute.
  82.  
  83.     Hue. The attribute of a visual sensation according to which an area appears
  84.     to be similar to one, or to proportions of two, of the perceived colors
  85.     red, yellow, green and blue.
  86.  
  87.     Colorfulness. The attribute of a visual sensation according to which an
  88.     area appears to exhibit more or less of its hue. You can go from a sky blue
  89.     to a deep blue by changing this attribute.
  90.  
  91.     So, a color is a visual sensation produced by a stimulus which is a
  92.     specific SPD. It should be noted however, that two different SPD's may
  93.     produce the same visual sensation - an effect known as metarmerism.
  94.  
  95.     What is a color space?
  96.  
  97.     A color space is a method by which we can specify, create and visualise
  98.     color. As human's, we may define a color by its attributes of brightness,
  99.     hue and colorfulness. A computer will define a color in terms of the
  100.     excitations of red, green and blue phosphors on the CRT faceplate. A
  101.     printing press defines color in terms of the reflectance and absorbance of
  102.     cyan, magenta, yellow and black inks on the paper.  
  103.  
  104.     If we imagine that each of the three attributes used to describe a color
  105.     are axes in a three dimensional space then this defines a color space.
  106.     The colors that we can percieve can be represented by the CIE system, other
  107.     color spaces are subsets of this perceptual space. For instance RGB color
  108.     space, as used by television displays, can be visualised as a cube with
  109.     red, green and blue axes. This cube lies within our perceptual space, since
  110.     the RGB space is smaller and represents less colors than we can see. CMY
  111.     space would be represented by a second cube, with a different orientation
  112.     and a different position within the perceptual space.
  113.  
  114.     So, a color space is a mathematical representation of our perceptions. E.g.
  115.     RGB is a tri-dimensional space with red, green and blue axes. It's useful
  116.     to think so because computers are in fond of numbers and equations...
  117.  
  118.     Why is there more than one color space?
  119.  
  120.     Different color spaces are better for different applications, some
  121.     equipment has limiting factors that dictate the size and type of color
  122.     space that can be used.  Some color spaces are perceptually linear, i.e. a
  123.     10 unit change in stimulus will produce the same change in perception
  124.     wherever it is applied. Many color spaces, particularly in computer
  125.     graphics are not linear in this way. Some color spaces are intuitive to
  126.     use, i.e. it is easy for the user to navigate within them and creating
  127.     desired colors is relativly easy. Finally, some color spaces are device
  128.     dependent while others are not (so called device independent).
  129.  
  130.     What's the difference between device dependent and device independent?
  131.  
  132.     A device dependent color space is a color space where the color produced
  133.     depends on the equipment and the set-up used to produce it. For example the
  134.     color produced using pixel values of [rgb = 250,134,67] will alter as you
  135.     change the brightness and contrast on your display. In the same way if you
  136.     change your monitor the red, green and blue phosphors will have slightly
  137.     different SPD's and the color produced will change. Thus RGB is a color
  138.     space that is dependent on the system being used, it is device dependent.
  139.     A device indepent color space is one where the coordinates used to specify
  140.     the color will produce the same color wherever they are applied.
  141.     An example of a device independent color space (if it has been implemented
  142.     properly) is the CIE L*a*b* color space (known as CIELab). This is based on
  143.     the HVS as described by the CIE system (see below to know what CIE stands
  144.     for).
  145.  
  146.     Another way of looking a device dependancy is to imagine our RGB cube
  147.     within our perceptual color space. We define a color by the values on the
  148.     three axes. However the exact color will depend on the position of the cube
  149.     within the perceptual color space, move the cube (by changing the set-up)
  150.     and the color will change even if the RGB values remain the same.
  151.     Some device dependent color spaces have their position within CIE space
  152.     defined, these are known as device callibrated color spaces and are a kind
  153.     of half way house between dependent and independent color spaces. For
  154.     example, a graphics file that contains colorimetric information, i.e. the
  155.     white point, transfer functions, and phosphor chromaticities, would enable
  156.     device dependent RGB data to be modified for whatever device was being used
  157.     - i.e. callibrated to specific devices. In other words, if you have a
  158.     device independent color space, you must adapt your device as defined in
  159.     the color space and not the color space to the device.
  160.  
  161.     What is a color gamut?
  162.  
  163.     A color gamut is the boundary of the color space. Gamut's are best
  164.     shown and evaluated using the CIE system.
  165.  
  166.     What is the CIE System?
  167.  
  168.     The CIE has defined a system that classifies color according to the HVS (it
  169.     started producing specifications in 1931). Using this system we can specify
  170.     any color in terms of its CIE coordinates. 
  171.  
  172.     The CIE system works by weighting the SPD of an object in terms of three
  173.     color matching functions. These functions are the sensitivities of a
  174.     standard observer to light of different wavelengths. The weighting is 
  175.     performed over the visual spectrum, from around 360nm to 830nm in 1nm 
  176.     intervals. However, the illuminant, and lighting and viewing geometry are 
  177.     carefully defined. This process produces three CIE tri-stimulus values, 
  178.     XYZ, which describe the color. 
  179.  
  180.     There are many measures that can be derived from the tri-stimulus values,
  181.     these include chromaticity coordinates and color spaces.
  182.  
  183.     What color space should I use?
  184.  
  185.     That depends on what you want to do, but heres a list of the pros and cons
  186.     of some of the more common, computer related, color spaces - we will see
  187.     in item 5 how to convert the (most common) color spaces between
  188.     themselves - :
  189.  
  190.     RGB (Red Green Blue)
  191.  
  192.     Additive color system based on trichromatic theory, commonly used by CRT
  193.     displays where proportions of excitation of red, green and blue emmiting
  194.     phosphors produce colors when visually fused. Easy to implement, non
  195.     linear, device dependent, unintuitive, common (used in television cameras,
  196.     computer graphics etc).
  197.  
  198.     CMY(K) (Cyan Magenta Yellow (Black))
  199.  
  200.     Subtractive color. Used in printing and photography. Printers often include
  201.     the fourth component, black ink, to improve the color gamut (by increasing
  202.     the density range), improving blacks, saving money and speeding drying (less
  203.     ink to dry). Fairly easy to implement, difficult to transfer *properly* from
  204.     RGB (simple transforms are, well, simple), device dependent, non-linear,
  205.     unintuitive.
  206.  
  207.     HSL (Hue Saturation and Lightness)
  208.  
  209.     This represents a wealth of similar color spaces, alternatives include HSI
  210.     (intensity), HSV (value), HCI (chroma / colorfulness),  HVC, TSD (hue
  211.     saturation and darkness) etc etc. All these color spaces are non-linear
  212.     transforms from RGB they are thus, device dependent, non-linear but very
  213.     intuitive. In addition the seperation of the luminance component has
  214.     advantages in image processing and other applications. (But take care, the
  215.     complete isolation of the separate components will require a space
  216.     optimised for your device. See later notes on CIE color spaces)
  217.  
  218.     YIQ, YUV, YCbCr, YCC  (Luminance - Chrominance)
  219.  
  220.     These are the television transmission color spaces (YIQ and YUV analogue
  221.     (NTSC and PAL) and YCbCr digital). They separate luminance from chrominance
  222.     (lightness from color) and are useful in compression and image processing
  223.     applications. YIQ and YUV are, if used according to their relative
  224.     specifications, linear. They are all device dependent and, unless you are a
  225.     TV engineer, unintuitive. Kodaks PhotoCD system uses a type of YCC color
  226.     space, PhotoYCC, which is a device calibrated color space.
  227.  
  228.     CIE 
  229.  
  230.     The HVS based color specification system. There are two CIE based color
  231.     spaces, CIELuv and CIELab. They are near linear (as close as any color
  232.     space is expected to sensibly get), device independent (unless your in the
  233.     habit of swapping your eye balls with aliens), but not very intuitive to
  234.     use. 
  235.  
  236.     From CIELuv you can derive CIELhs or CIELhc where h is the hue (an angle),
  237.     s the saturation and c the chroma. CIELuv has an associated chromaticity
  238.     diagram, a two dimensional chart which makes additive color mixing very
  239.     easy to visualise, hence CIELuv is widely used in additive color
  240.     applications, like television.
  241.     CIELab has no associated two dimensional chromaticity diagram and no 
  242.     correlate of saturation so only Lhc can be used.
  243.  
  244.     Since there is such a wide variet of color spaces, its useful to understand
  245.     a bit more about them and how to convert between them.
  246.  
  247.     The color space conversions are essentially provided for programmers. If
  248.     you are a specialist then skip to the references in item 6. Many of the
  249.     conversions are based on linear matrix transforms. (Was it Jim Blinn who
  250.     said that any problem in computer graphics can be solved by a matrix 
  251.     transform ?). As an example:
  252.  
  253.     E.g. RGB -> CIE XYZccir601-1 (D65) provides the following matrix of numbers
  254.     (see item 5.3):
  255.     |  0.607   0.174   0.200 |
  256.     |  0.299   0.587   0.114 |
  257.     |  0.000   0.066   1.111 |
  258.     and CIE XYZccir601-1 (D65) -> RGB provides the following matrix:
  259.     |  1.910  -0.5338 -0.2891  |
  260.     | -0.9844  1.9990 -0.0279 |
  261.     | 0.0585  -0.1187 -0.9017 |
  262.     
  263.     These two matrices are the (approximate) inversion of each other. If you
  264.     are a beginner in this mathematical stuff, skip the previous explainations,
  265.     and just use the results...
  266.  
  267.     Other definitions.
  268.  
  269.     Photometric terms:  illuminance   - luminous flux per unit area incident on
  270.                                         a surface
  271.                         luminance     - luminous flux per unit solid angle and
  272.                                         per unit projected area, in a given
  273.                                         direction, at a point on a surface.
  274.                         luminous flux - radient flux weighted by the V(landa)
  275.                                         function.
  276.                                     I.e. weighted by the eye's sensitivity.
  277.                         radient flux  - total power / energy of the incident
  278.                                         radiation.
  279.  
  280.     Other terms:        brightness    - the human sensation by which an area
  281.                                         exhibits more or less light.
  282.                         lightness     - the sensation of an area's brightness
  283.                                         relative to  a reference white in the
  284.                                         scene.
  285.                         chroma        - the colorfulness of an area relative to
  286.                                         the brightness of a reference white.
  287.                         saturation    - the colorfulness of an area relative to
  288.                                         its  brightness. 
  289.  
  290.     Note: This list is not exhaustive, some terms have alternative meanings
  291.     but we assume these to be the fundamentals.
  292.  
  293. 3 - What is an image based on a color look-up table (LUT)?
  294.  
  295.     All of the pictures don't use the full color space. That's why we often
  296.     use another scheme to improve the encoding of the picture (especially
  297.     to get a file which takes less space). To do so, you have two
  298.     possibilities:
  299.     - You reduce the bits/sample. It means you use less bits for each component
  300.     that describe the color. The colors are described as direct colors, it
  301.     means that all the pixels (or vectors, for vectorial descriptions) are
  302.     directly stored with the full components. For example, with a RGB (see
  303.     item 5.1 to know what RGB is) bitmapped image with a width of 5 pixels and
  304.     a height of 8 pixels, you have:
  305.     (R11,G11,B11) (R12,G12,B12) (R13,G13,B13) (R14,G14,B14) (R15,G15,B15)
  306.     (R21,G21,B21) (R22,G22,B22) (R23,G23,B23) (R24,G24,B24) (R25,G25,B25)
  307.     (R31,G31,B31) (R32,G32,B32) (R33,G33,B33) (R34,G34,B34) (R35,G35,B35)
  308.     (R41,G41,B41) (R42,G42,B42) (R43,G43,B43) (R44,G44,B44) (R45,G45,B45)
  309.     (R51,G51,B51) (R52,G52,B52) (R53,G53,B53) (R54,G54,B54) (R55,G55,B55)
  310.     (R61,G61,B61) (R62,G62,B62) (R63,G63,B63) (R64,G64,B64) (R65,G65,B65)
  311.     (R71,G71,B71) (R72,G72,B72) (R73,G73,B73) (R74,G74,B74) (R75,G75,B75)
  312.     (R81,G81,B81) (R82,G82,B82) (R83,G83,B83) (R84,G84,B84) (R85,G85,B85)
  313.     where Ryx, Gyx, Byx are respectively the Red, Green, and Blue components
  314.     you need to render a color for the pixel located at (x;y).
  315.     - You use a palette. In this case, all the colors are stored in a table
  316.     called a palette. This table is usually small. And the components of all
  317.     the colors for each pixel (or the vector data) are removed to be replaced
  318.     with a number. This number is an index in the palette. It explains why we
  319.     call the palette, a color look-up table (LUT or CLUT).
  320.  
  321. 4 - What is this gamma component?
  322.  
  323.     Many image processing operations, and also color space transforms that
  324.     involve device independent color spaces, like the CIE system based ones,
  325.     must be performed in a linear luminance domain.
  326.     By this we really mean that the relationship between pixel values specified
  327.     in software and the luminance of a specific area on the CRT display must be
  328.     known. CRTa will have a non-linear response.
  329.     The luminance of a CRT is generally modelled using a power function with an
  330.     exponent, gamma, somewhere between 2.2 [NTSC and SMPTE specifications] and
  331.     2.8 [as given by Hunt and Sproson]. Recent measurements performed at the
  332.     BBC in the UK (Richard Salmon and Alan Roberts) have shown that the actual
  333.     value of gamma is very dependent upon the accurate setting of the CRTs
  334.     black level. For correctly set-up CRTs gamma is 2.35 +/- 0.1. This
  335.     relationship is given as follows:
  336.  
  337.     Luminance = voltage ^ gamma
  338.  
  339.     Where luminance and voltage are normalised. For Liquid Crystal Displays the
  340.     response is more closely followed by an "S" shaped curve with a vicious
  341.     hook near black and a slow roll-off near white.
  342.  
  343.     In order to display image information as linear luminance we need to modify
  344.     the voltages sent to the CRT. This process stems from television systems
  345.     where the camera and reciever had different transfer functions (which,
  346.     unless corrected, would cause problems with tone reproduction). The
  347.     modification applied is known as gamma correction and is given below:
  348.  
  349.     New_Voltage = Old_Voltage ^ (1/gamma)
  350.  
  351.     (Both voltages are normalised and gamma is the value of the exponent of the
  352.     power function that most closely models the luminance-voltage relationship
  353.     of the display being used.)
  354.  
  355.     For a color computer system we can replace the voltages by the pixel
  356.     values selected, this of course assumes that your graphics card converts
  357.     digital values to analogue voltages in a linear way. (For precision work
  358.     you should check this). The color relationships are:
  359.  
  360.     Red  = a* (Red' ^gamma) +b
  361.     Green= a* (Green' ^gamma) +b
  362.     Blue = a* (Blue' ^gamma) +b
  363.  
  364.     where Red', Green', and Blue' are the normalised input RGB pixel values and
  365.     Red, Green, and Blue are the normalised gamma corrected signals sent to the
  366.     graphics card. The values of the constants a and b componsate for the
  367.     overall system gain and system offset respectively. (Essentially gain is
  368.     contrast and offset is intensity.) For basic applications the value of a,
  369.     b and gamma can be assumed to be consistent between color channels,
  370.     however for precise applications they must be measured for each channel
  371.     separatley.
  372.            
  373.     It is common to perform gamma correction by calculating the corrected value
  374.     for each possible input value and storing this in an array as a Look Up
  375.     Table (see item 3). In some cases the LUT is part of the graphics card, in
  376.     others it needs to be implemented in software.
  377.  
  378.     Research by Cowan and Post (see references in item 6) has shown that not
  379.     all CRT displays can be accurately modelled by a simple power relationship.
  380.     Cowan found errors of up to 100% between measured and calculated values. To
  381.     prevent this a simple LUT generating gamma correction function cannot be
  382.     used. The best method is to measure the absolute luminance of the display
  383.     at various pixel values and to linearily interpolate between them to
  384.     produce values for the LUT.
  385.  
  386.     It should be noted at this point that correct gamma correction depends on a
  387.     number of factors, in addition to the characteristics of the CRT display.
  388.     These include the gamma of the input device (scanner, video grabber etc),
  389.     the viewing conditions and target tone reproduction characteristics. In
  390.     addition linear luminance is not always desirable, for computer generated
  391.     images a system which is linear with our visual perception may be
  392.     preferable. If luminance is represented by L then lightness, our visual
  393.     sensation of an objects brightness relative to a similarly illuminated
  394.     area that appears white is given by L:
  395.  
  396.     { L=116*((Y/Yn)^(1/3))-16 if Y/Yn>0.008856
  397.     { L=903.3*Y/Yn            if Y/Yn<=0.008856
  398.  
  399.     This relationship is in fact the CIE 1976 Lightness (L*) equation which will
  400.     be discussed in section 5.4. 
  401.  
  402.     Most un-gamma corrected displays have a near linear L* response, thus for
  403.     computer generated applications, or applications where pixel values in an
  404.     image need to be intuitivly changed, this set up may be better.
  405.  
  406.     Note: Gamma correction performed in integer maths is prone to large
  407.     quantisation errors. For example, applying a gamma correction of 1/2.2
  408.     to an image with an original gamma of one (linear luminance) produced a
  409.     drop in the number of grey levels from 245 to 196. Take care not to alter
  410.     the transfer characteristics more than is necessary, if you need to gamma
  411.     correct images try to keep the originals so that you can pass them on to
  412.     others without passing on the degradations that you've produced ;-).
  413.  
  414.     In some image file formats or in graphics applications in general, you
  415.     need sometimes some other kinds of correction. These corrections provide
  416.     some specific processings rather than true gamma correction curves.
  417.     This is often the case, for examples, with the printing devices or
  418.     in animation. In the first case, it is interesting to specify that a color
  419.     must be re-affected in order you get a better rendering, as we see it later
  420.     in CMYK item. In the second case, some animations can need an extra
  421.     component associated to each pixel. This component can be, for example,
  422.     used as a transparency mask. We *improperly* call this extra component
  423.     gamma correction.
  424.     -> not to to be confused with gamma.
  425.  
  426. 5 - Color space conversions
  427.  
  428.     Except an historical point of view, most of you are - I hope - interested
  429.     in color spaces to make renderings and, if possible, on your favorite
  430.     computer. Most of computers display in the RGB color space but you may need
  431.     sometimes the CMYK color space for printing, the YCbCr or CIE Lab to
  432.     compress with JPEG scheme, and so on. That is why we are going to see,
  433.     from here, what are all these color spaces and how to convert them from one
  434.     to another (and primary from one to RGB and vice-versa, this was my purpose
  435.     when I started this FAQ).
  436.  
  437.     I provide the color space conversions for programmers. The specialists
  438.     don't need most of these infos or they can give a glance to all the stuff
  439.     and read carefully the item 6. Many of the conversions are based on linear
  440.     functions. The best example is given in item 5.3. These conversions can
  441.     be seen in matrices. A matrix is in mathematics an array of values. And to
  442.     go from one to another space color, you just make a matrix inversion.
  443.     E.g. RGB -> CIE XYZrec601-1 (C illuminant) provides the following matrix
  444.     of numbers (see item 5.3):
  445.     |  0.607   0.174   0.200 |
  446.     |  0.299   0.587   0.114 |
  447.     |  0.000   0.066   1.116 |
  448.     and CIE XYZrec601-1 (C illuminant) -> RGB provides the following matrix:
  449.     |  1.910  -0.532  -0.288 |
  450.     | -0.985   1.999  -0.028 |
  451.     |  0.058  -0.118   0.898 |
  452.     These two matrices are the (approximative) inversion of each other.
  453.     If you are a beginner in this mathematical stuff, skip the previous
  454.     explainations, and just use the result...
  455.  
  456. 5.1 - RGB, CMY, and CMYK
  457.  
  458.     The most popular color spaces are RGB and CMY. These two acronyms stand
  459.     for Red-Green-Blue and Cyan-Magenta-Yellow. They're device-dependent.
  460.     The first is normally used on monitors, the second on printers.
  461.  
  462.     RGB are known as additive primary colors, because a color is produced by
  463.     adding different qunatities of the three components, red, green, and blue.
  464.  
  465.     CMY are known as subtractive (or secondary) colors, because the color is
  466.     generated by subtracting different quantities of cyan, magenta and yellow
  467.     from white light.
  468.  
  469.     The primaries used by artists, red, yellow and blue are different because
  470.     they are concerned with mixing pigments rather than lights or dyes.
  471.  
  472.     RGB -> CMY                | CMY -> RGB
  473.     Red   = 1-Cyan    (0<=Cyan<=1)    | Cyan    = 1-Red (0<=Red<=1)
  474.     Green = 1-Magenta    (0<=Magenta<=1)    | Magenta = 1-Green (0<=Green<=1)
  475.     Blue  = 1-Yellow    (0<=Yellow<=1)    | Yellow  = 1-Blue (0<=Blue<=1)
  476.  
  477.     On printer devices, a component of black is added to the CMY, and the
  478.     second color space is then called CMYK (Cyan-Magenta-Yellow-blacK). This
  479.     component is actually used because cyan, magenta, and yellow set up to the
  480.     maximum should produce a black color. (The RGB components of the white are
  481.     completly substracted from the CMY components.) But the resulting color
  482.     isn't physically a 'true' black. The transforms from CMY to CMYK (and vice
  483.     versa) are given as shown below:
  484.     CMY -> CMYK                | CMYK -> CMY
  485.     Black=minimum(Cyan,Magenta,Yellow)    | Cyan=minimum(1,Cyan*(1-Black)+Black)
  486.     Cyan=(Cyan-Black)/(1-Black)        | Magenta=minimum(1,Magenta*(1-Black)+Black)
  487.     Magenta=(Magenta-Black)/(1-Black)    | Yellow=minimum(1,Yellow*(1-Black)+Black)
  488.     Yellow=(Yellow-Black)/(1-Black)    |
  489.  
  490.     Note, these differ to the descriptions often given, for example in Adobe
  491.     Postscript. For more information see FIELD in section 6. This is because
  492.     Adobe doesn't choose to use the most recent equations. (I don't know why!)
  493.  
  494.     RGB -> CMYK                | CMYK -> RGB
  495.     Black=minimum(1-Red,1-Green,1-Blue)    | Red=1-minimum(1,Cyan*(1-Black)+Black)
  496.     Cyan=(1-Red-Black)/(1-Black)    | Green=1-minimum(1,Magenta*(1-Black)+Black)
  497.     Magenta=(1-Green-Black)/(1-Black)    | Blue=1-minimum(1,Yellow*(1-Black)+Black)
  498.     Yellow=(1-Blue-Black)/(1-Black)    |
  499.  
  500.     Of course, I assume that C, M, Y, K, R, G, and B  have a range of [0;1].
  501.  
  502. 5.2 - HSI and related color spaces
  503.  
  504.     The representation of the colors in the RGB and CMY(K) color spaces are
  505.     designed for specific devices. But for a human observer, they are not
  506.     useful definitions. For user interfaces a more intuitive color space,
  507.     designed for the way we actually think about color is to be preferred.
  508.     Such a color space is HSI; Hue, Saturation and Intensity, which can be
  509.     thought of as an RGB cube tipped up onto one corner. The line from RGB=min
  510.     to RGB=max becomes verticle and is the intensity axis. The position of a
  511.     point on the circumference of a circle around this axis is the hue and the
  512.     saturation is the radius from the central intensity axis to the color.
  513.  
  514.                  Green
  515.                   /\
  516.                 /    \    ^
  517.               /V=1 x   \   \ Hue (angle, so that Hue(Red)=0, Hue(Green)=120, and Hue(blue)=240 deg)
  518.        Blue -------------- Red
  519.             \      |     /
  520.              \     |-> Saturation (distance from the central axis)
  521.               \    |   /
  522.                \   |  /
  523.                 \  | /
  524.                  \ |/
  525.                V=0 x (Intensity=0 at the top of the apex and =1 at the base of the cone)
  526.     
  527.     The big disadvantage of this model is the conversion which is mainly
  528.     because the hue is expressed as an angle. The transforms are given below:
  529.  
  530.     Hue = (Alpha-arctan((Red-intensity)*(3^0.5)/(Green-Blue)))/(2*PI)
  531.     with { Alpha=PI/2 if Green>Blue
  532.          { Aplha=3*PI/2 if Green<Blue
  533.          { Hue=1 if Green=Blue
  534.     Saturation = (Red^2+Green^2+Blue^2-Red*Green-Red*Blue-Blue*Green)^0.5
  535.     Intensity = (Red+Green+Blue)/3
  536.  
  537.     Note that you have to compute Intensity *before* Hue. If not, you must
  538.     assume that:
  539.     Hue = (Alpha-arctan((2*Red-Green-Blue)/((Green-Blue)*(3^0.5))))/(2*PI).
  540.  
  541.     I assume that H, S, L, R, G, and B are within range of [0;1].
  542.  
  543.     Another point of view of this cone is to project the coordinates onto the
  544.     base. The 2D projection is:
  545.     Red:   (1;0)
  546.     Green: (cos(120 deg);sin(120 deg)) = (-0.5; 0.866)
  547.     Blue:  (cos(240 deg);sin(240 deg)) = (-0.5;-0.866)
  548.     Now you need intermediate coordinates:
  549.     a = Red-0.5*(Green+Blue)
  550.     b = 0.866*(Green-Blue)
  551.     Finally, you have:
  552.     Hue = arctan2(x,y)/(2*PI) ; Just one formula, always in the correct quadrant
  553.     Saturation = (x^2+y^2)^0.5
  554.     Luminosity = (Red+Green+Blue)/3
  555.     This interesting point of view was provided by Christian Steyaert
  556.     (steyaert@vvs.innet.be).
  557.  
  558.     Another model close to HSI is HSL. It's actually a double cone with black
  559.     and white points placed at the two apexes of the double cone.
  560.     I don't provide formula, but feel free to send me the formula you could
  561.     find. ;-)
  562.  
  563.     Actually, here are many variations on HSI, e.g. HSL, HSV, HCI (chroma /
  564.     colorfulness), HVC, TSD (hue saturation and darkness) etc. But they all
  565.     do basically the same thing.
  566.  
  567. 5.3 - CIE XYZ and gray level pictures
  568.  
  569.     The CIE (presented in the item 2) has defined a human "Standard Observer",
  570.     based on measurements of the colour-matching abilities of the average human
  571.     eye. Using data from measurements made in 1931, a system of three primaries,
  572.     XYZ, was developed in which all visible colours can be represented using
  573.     only positive values of XY and Z. The Y primary is identical to Luminance,
  574.     X and Z give colouring information. This forms the basis of the CIE 1931
  575.     XYZ colour space, which is fundamental to all colorimetry. It is completely
  576.     device-independant and values are normally assumed to lie in the range
  577.     [0;1]. Colours are rarely specified in XYZ terms, it is far more common to
  578.     use "chromaticity coordinates" which are independant of the Luminance (Y).
  579.     The main advantage of CIE XYZ, and any color space or color definition
  580.     based on it, is that it is completely device independent. The main
  581.     disadvantage with CIE based spaces is the complexity of implementing them,
  582.     in addition some are not user intuitive. A complete description of the CIE
  583.     system is beyond the scope of this FAQ, we simply present useful formula to
  584.     convert between CIE values and between CIE and non-CIE color spaces. We
  585.     cannot recommend too highly that anyone wishing to implement any of the CIE
  586.     system in the digital domain reads the refs in section 6, specifically HUNT
  587.     1, SPROSON, BERNS and CIE 1.
  588.  
  589.     Chromaticity coordinates are derived from tristimulus values (the amounts
  590.     of the primaries) by normalising thus:
  591.  
  592.      x = X/(X+Y+Z)
  593.      y = Y/(X+Y+Z)
  594.      z = Z/(X+Y+Z)
  595.  
  596.     Chromaticity coordinates are *always* used in lower case. Because they have
  597.     been normalised, only two values are needed to specify the colour, and so z
  598.     is normally discarded (because x+y+z=1). Colours can be plotted on a
  599.     "chromaticity diagram" using x and y as coordinates, with Y (Luminance)
  600.     normal to the diagram. When a colour is specified in this form, it is
  601.     referred to as CIE 1931 Yxy. Tristimulus values can always be derived from
  602.     Yxy values:
  603.  
  604.     X = x*Y/y
  605.     Z = (1-x-y)*Y/y
  606.  
  607.     For scientists and programers, it is possible to convert between RGB as
  608.     displayed on a CRT and CIE tristimulus values.
  609.  
  610.     The first step is to ensure that you have either linear luminance
  611.     information, or that you know the transfer function (gamma correction) of
  612.     the display device. For further details on this see section 4. This will
  613.     give you the luminances of the red, green and blue phosphor emissions from
  614.     the red, green and blue pixel values that you specify.
  615.     The second stage is to perform a matrix transform (see item 5) to convert
  616.     the red, green, blue luminance information to CIE XYZ tristimulus values,
  617.     essentially. We can apply Grassman's Laws to establish conversion matrices
  618.     between the XYZ primaries and any other set of primaries, for instance (if
  619.     we consider RGB):
  620.  
  621.     |Red  |      -1 |X|         |X|       |Red  |
  622.     |Green| = |M|  *|Y|      and   |Y| = |M|*|Green|
  623.     |Blue |         |Z|         |Z|       |Blue |
  624.  
  625.     The matrix M is 3 by 3, and consists of the tristimulus values of the RGB
  626.     primaries in terms of the XYZ primaries (phosphors on *your* CRT).
  627.  
  628.     Ideally you would measure these - if you have a colorimeter or a
  629.     spectroradiometer / spectrophotometer handy. Alternativly you could assume
  630.     that your system corresponds to a particular specification, e.g. NTSC, and
  631.     use the figures given by the standard, however this is often not a valid
  632.     assumption - and if you need to make it it's probably not worth going to
  633.     the effort of implementing the transforms, the error's induced would outway
  634.     any advantages of the CIE system. The third method is to derive the figures
  635.     from other data.
  636.  
  637.     To solve this system we need some more data. The first data is the color
  638.     reference we use. With the CIE standard the reference of your rendering is
  639.     the white. White point is achromatic and is defined so that Y=1, and
  640.     Red=Green=Blue. To get the white point coordinates and put it into our
  641.     previous matrix system we use the CIE xyY diagram. This diagram is a 2D
  642.     diagram (based on tristimuli in regard with the wave lengths) where you get
  643.     a color as (x;y). To transform this 2D diagram into a 3D, we just consider:
  644.     z=1-(x+y)
  645.     X=x*Y/y
  646.     Z=z*Y/y
  647.     (Take care on these letters because these are case sensitive. Otherwise
  648.     you'd get unaccurate results!)
  649.     From there we must consider the coordinates of the vertices in your
  650.     triangle reference. The three vertices in your triangle reference are
  651.     of course the red, the green, and the blue in CIE xyY diagram. Those colors
  652.     are "pure values", it means the chromacity coordinates of red, green, and
  653.     blue are defined in the CIE xyY diagram:
  654.     Red:   (xr; yr; zr=1-(xr+yr))
  655.     Green: (xg; yg; zg=1-(xg+yg))
  656.     Blue:  (xb; yb; zb=1-(xb+yb))
  657.     And the white is defined as:
  658.     |Xn|   |r1 g1 b1|   |Redn  |   |r1 g1 b1|   |1|
  659.     |Yn| = |r2 g2 b2| * |Greenn| = |r2 g2 b2| * |1| (1)
  660.     |Zn|   |r3 g3 b3|   |Bluen |   |r3 g3 b3|   |1|
  661.     (1) becomes by invoking the white balance condition (Red=Green=Blue=1 for
  662.     white):
  663.     |Xn|   |ar*xr ag*xg ab*xb|   |1|   |xr xg xb|   |ar|
  664.     |Yn| = |ar*yr ag*yg ab*yb| * |1| = |yr yg yb| * |ag| (2)
  665.     |Zn|   |ar*zr ag*zg ab*zb|   |1|   |zr zg zb|   |ab|
  666.     But Xn, Yn, and Zn are also defined as (xn;yn) from the CIE xyY diagram:
  667.     zn=1-(xn+yn)
  668.     Xn=xn*Yn/yn=xn/yn
  669.     Yn=1 (always for white!)
  670.     Zn=zn*Yn/yn=zn/yn
  671.     So (2) becomes:
  672.     |xn/yn|   |xr xg xb|   |ar|
  673.     |  1  | = |yr yg yb| * |ag| (3)
  674.     |zn/yn|   |zr zg zb|   |ab|
  675.     Now, xn, yn, zn, xr, yr, zr, xg, yg, zg, xb, yb, and zb are all known
  676.     because they are supplied. Multiplying the chromaticity coordinates by
  677.     these values gives the the matrix in equation (1) (with a HP pocket
  678.     computer, for example) and get ar, ag, and ab. So:
  679.     |X|   |xr*ar xg*ag xb*ab|   |Red  |
  680.     |Y| = |yr*ar yg*ag xb*ab| * |Green|
  681.     |Z|   |zr*ar zg*ag xb*ab|   |Blue |
  682.     Let's take some examples. The CCIR (Comite Consultatif International des
  683.     Radiocommunications) defined several recommendations. The most popular
  684.     (they shouldn't be used anymore, we will see later why) are CCIR 601-1
  685.     and CCIR 709.
  686.     The CCIR 601-1 is the old NTSC (National Television System Committee)
  687.     standard. It uses a white point called "C Illuminant". The white point
  688.     coordinates in the CIE xyY diagram are (xn;yn)=(0.310063;0.316158). The
  689.     red, green, and blue chromacity coordinates are:
  690.     Red:   xr=0.67 yr=0.33 zr=1-(xr+yr)=0.00
  691.     Green: xg=0.21 yg=0.71 zg=1-(xg+yg)=0.08
  692.     Blue:  xb=0.14 yb=0.08 zb=1-(xb+yb)=0.78
  693.     zn=1-(xn+yn)=1-(0.310063+0.316158)=0.373779
  694.     Xn=xn/yn=0.310063/0.316158=0.980722
  695.     Yn=1 (always for white)
  696.     Zn=zn/yn=0.373779/0.316158=1.182254
  697.     We introduce all that in (3) and get:
  698.     ar=0.981854
  699.     ab=0.978423
  700.     ag=1.239129
  701.     Finally, we have RGB -> CIE XYZccir601-1 (C illuminant):
  702.     |X|   |0.606881 0.173505 0.200336|   |Red  |
  703.     |Y| = |0.298912 0.586611 0.114478| * |Green|
  704.     |Z|   |0.000000 0.066097 1.116157|   |Blue |
  705.     Because I'm a programer, I preferr to round these values up or down (in
  706.     regard with the new precision) and I get:
  707.     RGB -> CIE XYZccir601-1 (C illuminant)    | CIE XYZccir601-1 (C illuminant) -> RGB
  708.     X = 0.607*Red+0.174*Green+0.200*Blue    | Red   =  1.910*X-0.532*Y-0.288*Z
  709.     Y = 0.299*Red+0.587*Green+0.114*Blue    | Green = -0.985*X+1.999*Y-0.028*Z
  710.     Z = 0.000*Red+0.066*Green+1.116*Blue    | Blue  =  0.058*X-0.118*Y+0.898*Z
  711.     The other common recommendation is the 709. The white point is D65 and have
  712.     coordinates fixed as (xn;yn)=(0.312713;0.329016). The RGB chromacity
  713.     coordinates are:
  714.     Red:   xr=0.64 yr=0.33
  715.     Green: xg=0.30 yg=0.60
  716.     Blue:  xb=0.15 yb=0.06
  717.     Finally, we have RGB -> CIE XYZccir709 (709):
  718.     |X|   |0.412411 0.357585 0.180454|   |Red  |
  719.     |Y| = |0.212649 0.715169 0.072182| * |Green|
  720.     |Z|   |0.019332 0.119195 0.950390|   |Blue |
  721.     This provides the formula to transform RGB to CIE XYZccir709 and vice-versa:
  722.     RGB -> CIE XYZccir709 (D65)            | CIE XYZccir709 (D65) -> RGB
  723.     X = 0.412*Red+0.358*Green+0.180*Blue    | Red   =  3.241*X-1.537*Y-0.499*Z
  724.     Y = 0.213*Red+0.715*Green+0.072*Blue    | Green = -0.969*X+1.876*Y+0.042*Z
  725.     Z = 0.019*Red+0.119*Green+0.950*Blue    | Blue  =  0.056*X-0.204*Y+1.057*Z
  726.     Recently (about one year ago), CCIR and CCITT were both absorbed into their
  727.     parent body, the International Telecommunications Union (ITU). So you must
  728.     *not* use CCIR 601-1 and CCIR 709 anymore. Furthermore, their names have
  729.     changed  respectively to Rec 601-1 and Rec 709 ("Rec" stands for
  730.     Recommendation). Here is the new ITU recommendation.
  731.     The white point is D65 and have coordinates fixed as (xn;yn)=(0.312713;
  732.     0.329016). The RGB chromacity coordinates are:
  733.     Red:   xr=0.64 yr=0.33
  734.     Green: xg=0.29 yg=0.60
  735.     Blue:  xb=0.15 yb=0.06
  736.     Finally, we have RGB -> CIE XYZitu (D65):
  737.     |X|   |0.430574 0.341550 0.178325|   |Red  |
  738.     |Y| = |0.222015 0.706655 0.071330| * |Green|
  739.     |Z|   |0.020183 0.129553 0.939180|   |Blue |
  740.     This provides the formula to transform RGB to CIE XYZitu and vice-versa:
  741.     RGB -> CIE XYZitu (D65)            | CIE XYZitu (D65) -> RGB
  742.     X = 0.431*Red+0.342*Green+0.178*Blue    | Red   =  3.063*X-1.393*Y-0.476*Z
  743.     Y = 0.222*Red+0.707*Green+0.071*Blue    | Green = -0.969*X+1.876*Y+0.042*Z
  744.     Z = 0.020*Red+0.130*Green+0.939*Blue    | Blue  =  0.068*X-0.229*Y+1.069*Z
  745.  
  746.     You should remember that these transforms are only valid if you have
  747.     equipment that matches these specifications, or you have images that you
  748.     know have been encoded to these standards. If this is not the case, the
  749.     CIE values you calculate will not be true CIE.
  750.  
  751.     All these conversions are presented noit just as demonstrations, they can
  752.     be used for conversion between systems ;-).
  753.     For example, in most of your applications you have true color images in RGB
  754.     color space. How to render them fastly on your screen or on your favorite
  755.     printer. This is simple. You can convert your picture instantaneously in
  756.     gray scale pictures see even in a black and white pictures as a magician.
  757.     To do so, you just need to convert your RGB values into the Y component.
  758.     Actually, Y is linked to the luminosity (Y is an achromatic component) and
  759.     X and Z are linked to the colorfulness (X and Z are two chromatic
  760.     components). Old softwares used Rec 601-1 and produced:
  761.     Gray scale=Y=(299*Red+587*Green+114*Blue)/1000
  762.     With Rec 709, we have:
  763.     Gray scale=Y=(213*Red+715*Green+72*Blue)/1000
  764.     Some others do as if:
  765.     Gray scale=Green (They don't consider the red and blue components at all)
  766.     Or, alternativly, you can average the three color components so:
  767.     Gray scale=(Red+Green+Blue)/3
  768.     But now all people *should* use the most accurate, it means ITU standard:
  769.     Gray scale=Y=(222*Red+707*Green+71*Blue)/1000
  770.     (That's very close to Rec 709!)
  771.     I made some personal tests and have sorted them in regard with the global
  772.     resulting luminosity of the picture (from my eye point of view!). The
  773.     following summary gives what I found ordered increasingly:
  774.     +-----------------------------+----------------+
  775.     |Scheme                       |Luminosity level|
  776.     +-----------------------------+----------------+
  777.     |Gray=Green                   |        1       |
  778.     |Gray=ITU (D65)               |        2       |
  779.     |Gray=Rec 709 (D65)           |        3       |
  780.     |Gray=Rec 601-1 (C illuminant)|        4       |
  781.     |Gray=(Red+Green+Blue)/3      |        5       |
  782.     +-----------------------------+----------------+
  783.     So softwares with Gray=Rec 709 (D65) produce a more dark picture than with
  784.     Gray=Green. Even if you theorically lose many details with Gray=Green
  785.     scheme, in fact, and with the 64-gray levels of a VGA card of a PC it is
  786.     hard to distinguish the loss.
  787.  
  788. 5.4 - CIE Luv and CIE Lab
  789.  
  790.     In 1976, the CIE defined two new color spaces to enable us to get more
  791.     uniform and accurate models. The first of these two color spaces is the
  792.     CIE Luv which component are L*, u* and v*. L* component defines the
  793.     luminancy, and u*, v* define chrominancy. CIE Luv is very used in
  794.     calculation of small colors or color differences, especially with additive
  795.     colors. The CIE Luv color space is defined from CIE XYZ.
  796.  
  797.     CIE XYZ -> CIE Lab
  798.     { L* = 116*((Y/Yn)^(1/3)) with Y/Yn>0.008856
  799.     { L* = 903.3*Y/Yn with Y/Yn<=0.008856
  800.     u* = 13*(L*)*(u'-u'n)
  801.     v* = 13*(L*)*(v'-v'n)
  802.     where u'=4*X/(X+15*Y*+3*Z) and v'=9*Y/(X+15*Y+3*Z)
  803.     and u'n and v'n have the same definitions for u' and v' but applied to the
  804.     white point reference. So, you have:
  805.     u'n=4*Xn/(Xn+15*Yn*+3*Zn) and v'n=9*Yn/(Xn+15*Yn+3*Zn)
  806.  
  807.     See also item 5.3 about Xn, Yn, and Zn.
  808.  
  809.     As CIE Luv, CIE Lab is a color space introduced by CIE in 1976. It's a new
  810.     incorporated color space in TIFF specs. In this color space you use three
  811.     components: L* is the luminancy, a* and b* are respectively red/blue and
  812.     yellow/blue chrominancies.
  813.     This color space is also defined with regard to the CIE XYZ color spaces.
  814.  
  815.     CIE XYZ -> CIE Lab
  816.     { L=116*((Y/Yn)^(1/3))-16 if Y/Yn>0.008856
  817.     { L=903.3*Y/Yn            if Y/Yn<=0.008856
  818.     a=500*(f(X/Xn)-f(Y/Yn))
  819.     b=200*(f(Y/Yn)-f(Z/Zn))
  820.     where { f(t)=t^(1/3) with Y/Yn>0.008856
  821.           { f(t)=7.787*t+16/116 with Y/Yn<=0.008856
  822.  
  823.     See also item 5.3 about Xn, Yn, and Zn.
  824.  
  825. 5.5 - LCH and CIE LSH
  826.  
  827.     CIELab and CIELuv both have a disadvantage if used for a user interface,
  828.     they are unintuitive to use. To solve this we can use CIE definitions for
  829.     chroma, c, Hue angle, h and saturation, s (see section 1). Hue, chroma and
  830.     saturation can be derived from CIELuv, and Hue and chroma but *NOT*
  831.     saturation can be derived from CIELab (this is because CIELab has no
  832.     associated chromaticity diagram and so no correlation of saturation is
  833.     possible).
  834.  
  835.     To distinguish between LCH derived from CIELuv and CIELab the values of
  836.     Hue, H, and Chroma, C, are given the subscripts uv if from CIELuv and ab
  837.     if from CIELab.
  838.  
  839.  
  840.     CIELab -> LCH
  841.  
  842.     L = L*
  843.     C = (a*^2+b*^2)^0.5
  844.     { H=0                                 if a=0
  845.     { H=(arctan((b*)/(a*))+k*PI/2)/(2*PI) if a#0 (add PI/2 to H if H<0)
  846.     { and { k=0 if a*>=0 and b*>=0
  847.           { or k=1 if a*>0 and b*<0
  848.           { or k=2 if a*<0 and b*<0
  849.           { or k=3 if a*<0 and b*>0
  850.  
  851.     CIELuv -> LCH
  852.  
  853.     L = L*
  854.     C = (u*^2 + v*^2)^0.5     or   C = L*s
  855.     H = arctan[(v*)/(u*)]
  856.     { H=0                                 if u=0
  857.     { H=(arctan((v*)/(u*))+k*PI/2)/(2*PI) if u#0 (add PI/2 to H if H<0)
  858.     { and { k=0 if u*>=0 and v*>=0
  859.           { or k=1 if u*>0 and v*<0
  860.           { or k=2 if u*<0 and v*<0
  861.           { or k=3 if u*<0 and v*>0
  862.  
  863.     CIELuv -> CIE LSH
  864.  
  865.     L = L*
  866.     s = 13[(u' - u'n)^2 + (v' - v'n)^2]^0.5
  867.     H = arctan[(v*)/(u*)]
  868.     { H=0                                 if u=0
  869.     { H=(arctan((v*)/(u*))+k*PI/2)/(2*PI) if u#0 (add PI/2 to H if H<0)
  870.     { and { k=0 if u*>=0 and v*>=0
  871.           { or k=1 if u*>0 and v*<0
  872.           { or k=2 if u*<0 and v*<0
  873.           { or k=3 if u*<0 and v*>0
  874.  
  875. 5.6 - The associated standards: YUV, YIQ, and YCbCr
  876.  
  877.     YUV and YIQ are standard color spaces used for analogue television
  878.     transmission. YUV is used in European TVs (PAL) and YIQ in North American
  879.     TVs (NTSC). These colors spaces are device-dependent, like RGB, but they
  880.     are callibrated. This is because the primaries used in these television
  881.     systems are specified by the relative standards authorities. Y is the
  882.     luminance component and is usually referred to as the luma component (it
  883.     comes from CIE standard), U,V or I,Q are the chrominance components (i.e.
  884.     the color signals).
  885.  
  886.     YUV uses D65 white point which coordinates are (xn;yn)=(0.312713;0.329016).
  887.     The RGB chromacity coordinates are:
  888.     Red:   xr=0.64 yr=0.33
  889.     Green: xg=0.29 yg=0.60
  890.     Blue:  xb=0.15 yb=0.06
  891.     See item 5.3 to understand why the above values.
  892.  
  893.     RGB -> YUV                    | YUV -> RGB
  894.     Y =  0.299*Red+0.587*Green+0.114*Blue    | Red   = Y+0.000*U+1.140*V
  895.     U = -0.147*Red-0.289*Green+0.436*Blue    | Green = Y-0.396*U-0.581*V
  896.     V =  0.615*Red-0.515*Green-0.100*Blue    | Blue  = Y+2.029*U+0.000*V
  897.  
  898.     RGB -> YIQ                    | YUV -> RGB
  899.     Y =  0.299*Red+0.587*Green+0.114*Blue    | Red   = Y+0.956*I+0.621*Q
  900.     I =  0.596*Red-0.274*Green-0.322*Blue    | Green = Y-0.272*I-0.647*Q
  901.     Q =  0.212*Red-0.523*Green+0.311*Blue    | Blue  = Y-1.105*I+1.702*Q
  902.  
  903.     YUV -> YIQ                | YIQ -> YUV
  904.     Y = Y (no changes)            | Y = Y (no changes)
  905.     I = -0.2676*U+0.7361*V        | U = -1.1270*I+1.8050*Q
  906.     Q =  0.3869*U+0.4596*V        | V =  0.9489*I+0.6561*Q
  907.  
  908.     Note that Y has a range of [0;1] (if red, green, and blue have a range of
  909.     [0;1]) but U, V, I, and Q can be as well negative as positive. I can't give
  910.     the range of U, V, I, and Q because it depends on precision from Rec specs
  911.     To avoid such problems, you'll preferr the YCbCr. This color space is
  912.     similar to YUV and YIQ without the disadvantages. Y remains the component
  913.     of luminancy but Cb and Cr become the respective components of blue and
  914.     red. Futhermore, with YCbCr color space you can choose your luminancy from
  915.     your favorite recommendation. The most popular are given below:
  916.     +----------------+---------------+-----------------+----------------+
  917.     | Recommendation | Coef. for red | Coef. for Green | Coef. for Blue |
  918.     +----------------+---------------+-----------------+----------------+
  919.     | Rec 601-1      | 0.2989        | 0.5867          | 0.1144         |
  920.     | Rec 709        | 0.2126        | 0.7152          | 0.0722         |
  921.     | ITU            | 0.2220        | 0.7067          | 0.0713         |
  922.     +----------------+---------------+-----------------+----------------+
  923.     RGB -> YCbCr
  924.     Y  = Coef. for red*Red+Coef. for green*Green+Coef. for blue*Blue
  925.     Cb = (Blue-Y)/(2-2*Coef. for blue)
  926.     Cr = (Red-Y)/(2-2*Coef. for red)
  927.     YCbCr -> RGB
  928.     Red   = Cr*(2-2*Coef. for red)+Y
  929.     Green = (Y-Coef. for blue*Blue-Coef. for red*Red)/Coef. for green
  930.     Blue  = Cb*(2-2*Coef. for blue)+Y
  931.     (Note that the Green component must be computed *after* the two other
  932.     components because Green component use the values of the two others.)
  933.     Usually, you'll need the following conversions based on Rec 601-1
  934.     for TIFF and JPEG works:
  935.     RGB -> YCbCr (with Rec 601-1 specs)        | YCbCr (with Rec 601-1 specs) -> RGB
  936.     Y=  0.2989*Red+0.5867*Green+0.1144*Blue    | Red=  Y+0.0000*Cb+1.4022*Cr
  937.     Cb=-0.1687*Red-0.3312*Green+0.5000*Blue    | Green=Y-0.3456*Cb-0.7145*Cr
  938.     Cr= 0.5000*Red-0.4183*Green-0.0816*Blue    | Blue= Y+1.7710*Cb+0.0000*Cr
  939.  
  940.     Additional note: Tom Lane provided me implementation of the Baseline JPEG
  941.     compression system but after a close look I understood that their values
  942.     was an approximation of the previous values (you should preferr mine ;-).).
  943.  
  944.     I assume Y is within the range [0;1], and Cb and Cr are within the range
  945.     [-0.5;0.5].
  946.  
  947. 5.7 - SMPTE-C RGB
  948.  
  949.     SMPTE is an acronym which stands for Society of Motion Picture and Television
  950.     Engineers. They give a gamma (=2.2 with NTSC, and =2.8 with PAL) corrected
  951.     color space with RGB components (about RGB, see item 5.1).
  952.     The white point is D65. The white point coordinates are (xn;yn)=(0.312713;
  953.     0.329016). The RGB chromacity coordinates are:
  954.     Red:   xr=0.630 yr=0.340
  955.     Green: xg=0.310 yg=0.595
  956.     Blue:  xb=0.155 yb=0.070
  957.     See item 5.3 to understand why the above values.
  958.     To get the conversion from SMPTE-C RGB to CIE XYZ or from CIE XYZ to
  959.     SMPTE-C RGB, you have two steps:
  960.     SMPTE-C RGB -> CIE XYZ (D65)        | CIE XYZ (D65) -> SMPTE-C RGB
  961.     - Gamma correction                | - Linear transformations:
  962.     Red=f1(Red')                | Red  = 3.5058*X-1.7397*Y-0.5440*Z
  963.     Green=f1(Green')                | Green=-1.0690*X+1.9778*Y+0.0352*Z
  964.     Blue=f1(Blue')                | Blue = 0.0563*X-0.1970*Y+1.0501*Z
  965.     where { f1(t)=t^2.2 whether t>=0.0        | - Gamma correction
  966.           { f1(t)-(abs(t)^2.2) whether t<0.0    | Red'=f2(Red)
  967.     - Linear transformations:            | Green'=f2(Green)
  968.     X=0.3935*Red+0.3653*Green+0.1916*Blue    | Blue'=f2(Blue)
  969.     Y=0.2124*Red+0.7011*Green+0.0866*Blue    | where { f2(t)=t^(1/2.2) whether t>=0.0
  970.     Z=0.0187*Red+0.1119*Green+0.9582*Blue    |       { f2(t)-(abs(t)^(1/2.2)) whether t<0.0
  971.  
  972. 5.8 - SMPTE-240M YPbPr (HD televisions)
  973.  
  974.     SMPTE gives a gamma (=0.45) corrected color space with RGB components
  975.     (about RGB, see item 5.1). With this space color, you have three components
  976.     Y, Pb, and Pr respectively linked to luminancy (see item 2), green, and
  977.     blue. The white point is D65. The white point coordinates are
  978.     (xn;yn)=(0.312713;0.329016). The RGB chromacity coordinates are:
  979.     Red:   xr=0.67 yr=0.33
  980.     Green: xg=0.21 yg=0.71
  981.     Blue:  xb=0.15 yb=0.06
  982.     See item 5.3 to understand why the above values.
  983.     Conversion from SMPTE-240M RGB to CIE XYZ (D65) or from CIE XYZ (D65) to
  984.     SMPTE-240M RGB, you have two steps:
  985.     YPbPr -> RGB                | RGB -> YPbPr
  986.     - Gamma correction                | - Linear transformations:
  987.     Red=f(Red')                    | Red  =1*Y+0.0000*Pb+1.5756*Pr
  988.     Green=f(Green')                | Green=1*Y-0.2253*Pb+0.5000*Pr
  989.     Blue=f(Blue')                | Blue =1*Y+1.8270*Pb+0.0000*Pr
  990.     where { f(t)=t^0.45 whether t>=0.0        | - Gamma correction
  991.           { f(t)-(abs(t)^0.45) whether t<0.0    | Red'=f(Red)
  992.     - Linear transformations:            | Green'=f(Red)
  993.     Y=  0.2122*Red+0.7013*Green+0.0865*Blue    | Blue'=f(Red)
  994.     Pb=-0.1162*Red-0.3838*Green+0.5000*Blue    | where { f(t)=t^(1/0.45) whether t>=0.0
  995.     Pr= 0.5000*Red-0.4451*Green-0.0549*Blue    |       { f(t)-(abs(t)^(1/0.45)) whether t<0.0
  996.  
  997. 5.9 - Xerox Corporation YES
  998.  
  999.     YES have three components which are Y (see Luminancy, item 2), E (chrominancy
  1000.     of red-green axis), and S (chrominancy of yellow-blue axis)
  1001.     Conversion from YES to CIE XYZ (D50) or from CIE XYZ (D50) to YES, you have two
  1002.     steps:
  1003.     YES -> CIE XYZ (D50)            | CIE XYZ (D50) -> YES
  1004.     - Gamma correction                | - Linear transformations:
  1005.     Y=f1(Y')                    | Y= 0.000*X+1.000*Y+0.000*Z
  1006.     E=f1(E')                    | E= 1.783*X-1.899*Y+0.218*Z
  1007.     S=f1(S')                    | S=-0.374*X-0.245*Y+0.734*Z
  1008.     where { f1(t)=t^2.2 whether t>=0.0        | - Gamma correction
  1009.           { f1(t)-(abs(t)^2.2) whether t<0.0    | Y'=f2(Y)
  1010.     - Linear transformations:            | E'=f2(E)
  1011.     X=0.964*Y+0.528*E-0.157*S            | S'=f2(S)
  1012.     Y=1.000*Y+0.000*E+0.000*S            | where { f2(t)=t^(1/2.2) whether t>=0.0
  1013.     Z=0.825*Y+0.269*E+1.283*S            |       { f2(t)-(abs(t)^(1/2.2)) whether t<0.0
  1014.  
  1015.     Conversion from YES to CIE XYZ (D65) or from CIE XYZ (D65) to YES, you have two
  1016.     steps:
  1017.     YES -> CIE XYZ (D65)            | CIE XYZ (D65) -> YES
  1018.     - Gamma correction                | - Linear transformations:
  1019.     Y=f1(Y')                    | Y= 0.000*X+1.000*Y+0.000*Z
  1020.     E=f1(E')                    | E=-2.019*X+1.743*Y-0.246*Z
  1021.     S=f1(S')                    | S= 0.423*X+0.227*Y-0.831*Z
  1022.     where { f1(t)=t^2.2 whether t>=0.0        | - Gamma correction
  1023.           { f1(t)-(abs(t)^2.2) whether t<0.0    | Y'=f2(Y)
  1024.     - Linear transformations:            | E'=f2(E)
  1025.     X=0.782*Y-0.466*E+0.138*S            | S'=f2(S)
  1026.     Y=1.000*Y+0.000*E+0.000*S            | where { f2(t)=t^(1/2.2) whether t>=0.0
  1027.     Z=0.671*Y-0.237*E-1.133*S            |       { f2(t)-(abs(t)^(1/2.2)) whether t<0.0
  1028.  
  1029.     Usually, you should use YES <-> CIE XYZ (D65) conversions because your
  1030.     screen and the usual pictures have D65 as white point. Of course, sometime
  1031.     you'll need the first conversions. Just take care on your pictures.
  1032.  
  1033. 5.10- Kodak Photo CD YCC
  1034.  
  1035.     The Kodak PhotoYCC color space was designed for encoding images with the
  1036.     PhotoCD system. It is based on both ITU Recommendations 709 and 601-1,
  1037.     having a color gamut defined by the ITU 709 primaries and a luminance -
  1038.     chrominance representation of color like ITU 601-1's YCbCr. The main
  1039.     attraction of PhotoYCC is that it calibrated color space, each image being
  1040.     tracable to Kodak's standard image-capturing device and the CIE Standard
  1041.     Illuminant for daylight, D65. In addition PhotoYCC provides a color gamut
  1042.     that is greater than that which can currently be displayed, thus it is
  1043.     suitable not only for both additive and subtractive (RGB and CMY(K))
  1044.     reproduction, but also for archieving since it offers a degree of
  1045.     protection against future progress in display technology.
  1046.     Images are scanned by a standardised image-capturing device, calibrated
  1047.     accoring to the type of photographic material being scanned. The scanner is
  1048.     sensitive to any color currently producable by photographic materials (and
  1049.     more besides). The image is encoded into a color space based on the ITU
  1050.     Rec 709 reference primaries and the CIE standard illuminant D65 (standard
  1051.     daylight). The extended color gamut obtainable by the PhotoCD system is
  1052.     achieved by allowing both positive and negative values for each primary.
  1053.     This means that whereas conventional ITU 709 encoded data is limited by
  1054.     the boundary of the triangle linking the three primaries (the color gamut),
  1055.     PhotoYCC can encode data outside the boundary, thus colors that are not
  1056.     realiseable by the CCIR primary set can be recorded. This feature means
  1057.     that PhotoCD stores more information (as a larger color gamut) than
  1058.     current display devices, such as CRT monitors and dye-sublimination
  1059.     printers, can produce. In this respect it is good for archieval storage of
  1060.     images since the pictures we see now will keep up with improving display
  1061.     technology.
  1062.     When an image is scanned it is stored in terms of the three reference
  1063.     primaries, these values, Rp, Gp and Bp are defined as follows:
  1064.  
  1065.     Rp = kr {integral of (Planda planda rlanda) dlanda}
  1066.     Gp = kg{integral of (Planda planda glanda) dlanda}
  1067.     Bp = kb{integral of (Planda planda blanda) dlanda}
  1068.  
  1069.     where Rp Gp and Bp are the CCIR 709 primaries although not constrained to
  1070.     positive values.
  1071.     kr kg kb are normalising constants;
  1072.     Planda is the spectral power distribution of the light source (CIE D65);
  1073.     planda is the spectral power distribution of the scene at a specific point
  1074.     (pixel);
  1075.     rlanda glanda and blanda are the spectral power distributions of the
  1076.     scanner components primaries.
  1077.  
  1078.     kr kg kb are specified as kr = 1 / {integral of (Planda * rlanda) dlanda}
  1079.     as similairly for kg and kb replacing rlanda with glanda and blanda
  1080.     respectivly.
  1081.  
  1082.     Let's stop with the theory and let's see how to make transforms.
  1083.     To be stored on a CD rom, the Rp Gp and Bp values are transformed into
  1084.     Kodak's PhotoYCC color space. This is performed in three stages. Firstly a
  1085.     non-linear transform is applied to the image signals (this is because
  1086.     scanners tend to be linear devices while CRT displays are not), the
  1087.     transform used is as follows:
  1088.     Y (see Luminancy, item 2) and C1 and C2 (both are linked to chrominancy).
  1089.     YC1C2->RGB                        | RGB->YC1C2
  1090.     - Gamma correction:                    | Y' =1.3584*Y
  1091.     Red  =f(red')                    | C1'=2.2179*(C1-156)
  1092.     Green=f(Green')                    | C2'=1.8215*(C2-137)
  1093.     Blue =f(Blue')                    | Red  =Y'+C2'
  1094.     where { f(t)=-1.099*abs(t)^0.45+0.999 if t<=-0.018    | Green=Y'-0.194*C1'-0.509*C2'
  1095.           { f(t)=4.5*t if -0.018<t<0.018        | Blue =Y'+C1'
  1096.           { f(t)=1.099*t^0.45-0.999 if t>=0.018        |
  1097.     - Linear transforms:                |
  1098.     Y' = 0.299*Red+0.587*Green+0.114*Blue        |
  1099.     C1'=-0.299*Red-0.587*Green+0.886*Blue        |
  1100.     C2'= 0.701*Red-0.587*Green-0.114*Blue        |
  1101.     - To fit it into 8-bit data:            |
  1102.     Y =(255/1.402)*Y'                    |
  1103.     C1=111.40*C1'+156                    |
  1104.     C2=135.64*C2'+137                    |
  1105.  
  1106.     Finally, I assume Red, Green, Blue, Y, C1, and C2 are in the range of
  1107.     [0;255]. Take care that your RGB values are not constrainted to positive
  1108.     values. So, some colors can be outside the Rec 709 display phosphor
  1109.     limit, it means some colors can be outside the trangle I defined in
  1110.     item 5.3. This can be explained because Kodak want to preserve some
  1111.     accurate infos, such as specular highlight information.
  1112.     You can note that the relations to transform YC1C2 to RGB is not exactly
  1113.     the reverse to transform RGB to YC1C2. This can be explained (from Kodak
  1114.     point of view) because the output displays are limited in the range of
  1115.     their capabilities.
  1116.  
  1117.     Converting stored PhotoYCC data to RGB 24bit data for display by computers
  1118.     on CRT's is achieved as follows;
  1119.  
  1120.     Firstly normal Luma and Chroma data are recovered:
  1121.  
  1122.     Luma     = 1.3584 * Luma(8bit)
  1123.     Chroma1     = 2.2179 * (Chroma1(8bit) - 156)
  1124.     Chroma2    = 1.8215 * (Chroma2(8bit) - 137)
  1125.  
  1126.     Assuming your display uses phosphors that are, or are very close to, ITU
  1127.     Rec 709 primaries in their chromaticities, then (* see below)
  1128.  
  1129.     Rdisplay    = L + Chroma2
  1130.     Gdisplay    = L - 0.194Chroma1 - 0.509Chroma2
  1131.     Bdisplay     = L + Chroma1
  1132.  
  1133.     Two things to watch are:
  1134.  
  1135.     a) this results in RGB values from 0 to 346 (instead of the more usual 0 to
  1136.     255) if this is simply ignored the resulting clipping will cause severe
  1137.     loss of highlight information in the displayed image, a look-up-table is
  1138.     usually used to convert these through a non-linear function to 8 bit data.
  1139.     For example:
  1140.  
  1141.     Y =(255/1.402)*Y'                    
  1142.     C1=111.40*C1'+156                    
  1143.     C2=135.64*C2'+137    
  1144.  
  1145.     b) if the display phosphors differ from CCIR 709 primaries then further
  1146.     conversion will be necessary, possibly through an intermediate device
  1147.     independedent color space such as CIE XYZ.
  1148.  
  1149.     * as a note -> do the phosphors need to match with regard to their
  1150.     chromaticities or is a spectral match required? Two different spectral
  1151.     distributed phosphors may have same chromaticites but may not be a
  1152.     metameric match since metarimerism only applies to spectral distribution of
  1153.     color matching functions.
  1154.     Another point to note is that PhotoCD images are chroma subsampled, that is
  1155.     to say for each 2 x 2 block of pixels, 4 luma and 1 of each chroma
  1156.     component are sampled (i.e. chroma data is averaged over 2x2 pixel blocks).
  1157.     This technique uses the fact that the HVS is less sensitive to luminance
  1158.     than chromaince diferences so color information can be stored at a lower
  1159.     precision without percievable loss in visual quality.
  1160.  
  1161. 6 - References (most of them are provided by Adrian Ford)
  1162.  
  1163.     "An inexpensive scheme for calibration of a color monitor in terms of CIE
  1164.     standard coordinates" W.B. Cowan, Computer Graphics, Vol. 17 No. 3, 1983
  1165.  
  1166.     "Calibration of a computer controlled color monitor", Brainard, D.H, Color 
  1167.     Research & Application, 14, 1, pp 23-34 (1989).
  1168.  
  1169.     "Color Monitor Colorimetry", SMPTE Recommended Practice RP 145-1987
  1170.  
  1171.     "Color Temperature for Color Television Studio Monitors", SMPTE Recommended
  1172.     Practice RP 37
  1173.  
  1174.     SPROSON: "Color Science in Television and Display Systems" Sproson, W, N, 
  1175.     Adam Hilger Ltd, 1983. ISBN 0-85274-413-7
  1176.     (Color measuring from soft displays.
  1177.     Alan Roberts and Richard Salmon talk about it as a reference)
  1178.  
  1179.     CIE 1: "CIE Colorimetry" Official recommendations of the International
  1180.     Commission on Illumination, Publication 15.2 1986
  1181.  
  1182.     BERNS: "CRT Colorimetry:Part 1 Theory and Practice, Part 2 Metrology",
  1183.     Berns, R.S., Motta, R.J. and Gorzynski, M.E., Color Research and
  1184.     Appliation, 18, (1993).
  1185.     (Adrian Ford talks about it as a must about CIE implementations on CRT's)
  1186.  
  1187.     "Effective Color Displays. Theory and Practice", Travis, D, Academic Press,
  1188.     1991. ISBN 0-12-697690-2
  1189.     (Color applications in computer graphics)
  1190.  
  1191.     FIELD: Field, G.G., Color and Its Reproduction, Graphics Arts Technical
  1192.     Foundation, 1988, pp. 320-9
  1193.     (Read this about CMY/CMYK)
  1194.  
  1195.     POYNTON: "Gamma and its disguises: The nonlinear mappings of intensity in
  1196.     perception, CRT's, Film and Video" C. A. Poynton, SMPTE Journal, December
  1197.     1993
  1198.  
  1199.     HUNT 1: "Measuring Color" second edition, R. W. G. Hunt, Ellis Horwood
  1200.     1991, ISBN 0-13-567686-x
  1201.     (Calculation of CIE Luv and other CIE standard colors spaces)
  1202.  
  1203.     "On the Gun Independance and Phosphor Consistancy of Color Video Monitors"
  1204.     W.B. Cowan N. Rowell, Color Research and Application, V.11 Supplement 1986
  1205.  
  1206.     "Precision requirements for digital color reproduction", M Stokes
  1207.     MD Fairchild RS Berns, ACM Transactions on graphics, v11 n4 1992
  1208.  
  1209.     CIE 2: "The colorimetry of self luminous displays - a bibliography" CIE
  1210.     Publication n.87, Central Bureau of the CIE, Vienna 1990
  1211.  
  1212.     HUNT 2: "The Reproduction of Color in PhotoGraphy, Printing and
  1213.     Television", R. W. G. Hunt, Fountain Press, Tolworth, England, 1987
  1214.  
  1215.     "Fully Utilizing Photo CD Images, Article No. 4, PhotoYCC Color Encoding
  1216.     and Compression Schemes" Eastman Kodak Company, Rochester NY (USA) (1993).
  1217.  
  1218. 7 - Comments and thanks
  1219.  
  1220.     Whenever you would like to comment or suggest me some informations about
  1221.     this or about the color space transformations in general, please use email:
  1222.     dbourgin@ufrima.imag.fr (David Bourgin)
  1223.  
  1224.     Special thanks to the following persons (there are actually many other
  1225.     people to cite) for contributing to valid, see even to write some part of
  1226.     the items:
  1227.     - Adrian Ford (ajoec1@westminster.ac.uk)
  1228.     - Tom Lane (Tom_Lane@G.GP.CS.CMU.EDU)
  1229.     - Alan Roberts and Richard Salmon (Alan.Roberts@rd.bbc.co.uk,
  1230.                                        Richard.Salmon@rd.eng.bbc.co.uk)
  1231.     - Grant Sayer (grants@research.canon.oz.au)
  1232.     - Steve Westland (coa23@potter.cc.keele.ac.uk)
  1233.  
  1234.     Note: I'm doing my national service on 4 next months, it means up to the
  1235.     begining of july. I'll try to read and answer to my e-mails.
  1236.     Thanks to not be in a hurry ;-).
  1237.  
  1238. ###########################################################
  1239.